package com.cx.mq.rabbitmq.config;

import com.cx.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 该对象中配置的ConfirmCallback和ReturnCallback均针对的是消息发送这个阶段
 * ReturnCallback在交换机到队列这个阶段起作用
 * (1)exchange到queue成功,则不回调return
 * (2)exchange到queue失败,则回调return(需设置mandatory=true,否则不回回调,消息就丢了)
 *
 * Author: chenxiaoyi
 * Date: 2020/8/8 16:07
 */
@Slf4j
@Component
public class ReturnCallBackImpl implements RabbitTemplate.ReturnCallback {

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println(JsonUtils.object2String(message));
        log.error("消息没到达队列---错误码[{}]:[{}],交换机:[{}],路由键:[{}]",replyCode,replyText,exchange,routingKey);

    }
}
